<script setup lang="ts">
import { utils, writeFile } from 'xlsx'
import raw_data from './data.json'

// const a = async () => {
//     console.log('🌈 —— data-file --->', raw_data)
//     const prez = raw_data.filter((row) =>
//         row.terms.some((term) => term.type === 'prez')
//     )

//     prez.forEach(
//         (row) =>
//             (row.start = row.terms.find((term) => term.type === 'prez').start)
//     )
//     prez.sort((l, r) => l.start.localeCompare(r.start))
//     const rows = prez.map((row) => ({
//         name: row.name.first + ' ' + row.name.last,
//         birthday: row.bio.birthday
//     }))

//     const worksheet = utils.json_to_sheet(rows)
//     const workbook = utils.book_new()
//     utils.book_append_sheet(workbook, worksheet, 'Dates')
//     utils.sheet_add_aoa(worksheet, [['Name', 'Birthday']], { origin: 'A1' })
//     const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10)
//     worksheet['!cols'] = [{ wch: max_width }]
//     writeFile(workbook, 'Presidents.xlsx', { compression: true })
//     // const worksheet = workbook.Sheets[workbook.SheetNames[0]];
//     /* generate and display HTML */
//     // const table = utils.sheet_to_html(worksheet);
//     // console.log('🌈 —— data-w --->', table)
// }

const initialData = [
    {
        a: '订单编号',
        b: '24h',
        c: '产品编号',
        d: 'hfsn',
    },
    {
        a: '订单数量',
        b: '12121',
        c: '生产数量',
        d: 'dwadaw',
    },
    {
        a: '生产工厂',
        b: 'ccc',
        c: '验货日期',
        d: '12312',
    },
    {
        a: '净毛重',
        b: '',
        c: '产品尺寸',
        d: '',
    },
]

const test = async () => {
    const ws = utils.aoa_to_sheet([
        ['QC验货报告', , , , , , , , , , ,],
        ['订单编号', '24HFE62201', '产品编号', 'HFSN-1012/1068'],
        [
            '订单数量',
            'HFSN-1012WH-48,EM-20,GY-58,NY-30,MS-40,1068BG-36,EM-26,DG-46,NY-36,BK-36',
            '生产数量',
            'HFSN-1012WH-48,EM-20,GY-58,NY-30,MS-40,1068BG-36,EM-26,DG-46,NY-36,BK-36',
        ],
        ['生产工厂', '安吉创科', '验货日期', '11-1-2024'],
        ['净毛重', '', '产品尺寸', ''],
    ])
    ws['!merges'] = [
        { s: { c: 0, r: 0 }, e: { c: 4, r: 0 } }, // A1:B2
        { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }, // C1:C2
        { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }, // A3:B3
        { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }, // D1:D2
        { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }, // A4:B4
    ]
    // const worksheet = utils.json_to_sheet(initialData)
    const workbook = utils.book_new()
    utils.book_append_sheet(workbook, ws, 'QC验货报告')
    // utils.sheet_add_aoa(worksheet, [['a', 'b', 'c', 'd']], { origin: 'A1' })
    writeFile(workbook, 'pro.xlsx', { compression: true })
}
</script>

<template>
    <div class="father-container base-card">
        <ElButton @click="test">xlsx</ElButton>
    </div>
</template>

<style scoped>
.father-container {
}
</style>
